package com.hd.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import net.sf.json.JSONObject;
/**
 * 通过经纬度获取距离、通过经纬度获取地址，通过地址获取经纬度
 * @author hzhh123
 * @Date 2019年3月4日 下午12:01:38
 */
public class LocationUtils {
	private static double EARTH_RADIUS = 6378.137;
	
	private static String BAIDU_API_KEY="3ecea51f560650b1ed8a4b99808f52e8";
	 
	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}
 
	/**
	 * 通过经纬度获取距离(单位：米)
	 * 
	 * @param lat1
	 * @param lng1
	 * @param lat2
	 * @param lng2
	 * @return 距离
	 */
	public static double getDistance(double lat1, double lng1, double lat2,
			double lng2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000d) / 10000d;
		s = s * 1000;
		return s;
	}
	
	/**
	 *  根据经纬度获取位置信息
	 * @param latitude 纬度
	 * @param longitude 经度
	 * @return
	 */
	public static String GetAddressByLocaation(double latitude, double longitude) {
		String add = "";
		String addr="";
		String url = String.format("http://api.map.baidu.com/geocoder?location=%s,%s&output=json&key="+BAIDU_API_KEY, latitude,
				longitude);
		URL myURL = null;
		URLConnection httpsConn = null;
		try {
			myURL = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			httpsConn = (URLConnection) myURL.openConnection();
			if (httpsConn != null) {
				InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
				BufferedReader br = new BufferedReader(insr);
				String data = null;
				while ((data = br.readLine()) != null) {
					add = add + data;
				}
				insr.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		//System.out.println(add);
		
//		Map<String, String> map = new HashMap<String, String>();
		JSONObject jsonObject = JSONObject.fromObject(add);
		if (jsonObject.getString("status").equals("OK")) {
			JSONObject object = (JSONObject) jsonObject.get("result");
			System.out.println(object);
			addr=object.getString("formatted_address");
		}
		return addr;
	}
	/**
	 * 通过地址查询经纬度
	 * @param address
	 * @return
	 */
	public static String getLocation(String address) {
		String location="";
		String result="";
		String url=String.format("http://api.map.baidu.com/geocoder/v2/?address=%s&output=json&ak="+BAIDU_API_KEY, address);
		URL myURL = null;
		URLConnection httpsConn = null;
		try {
			myURL = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			httpsConn = (URLConnection) myURL.openConnection();
			if (httpsConn != null) {
				InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
				BufferedReader br = new BufferedReader(insr);
				String data = null;
				while ((data = br.readLine()) != null) {
					result = result + data;
				}
				insr.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSONObject jsonObject = JSONObject.fromObject(result);
		if (jsonObject.getInt("status")==0) {
			JSONObject object = (JSONObject) jsonObject.get("result");
			System.out.println(object);
			JSONObject obj=(JSONObject) object.get("location");
			location=obj.getString("lat")+","+obj.getString("lng");
		}
		System.out.println("纬度经度="+location);
		return location;
	}
	public static void main(String[] args) {
		double distance = getDistance(25.073167, 102.746985
				,25.072388, 102.745196);
		System.out.println("距离" + distance / 1000 + "km");
		String location = GetAddressByLocaation(25.070815,102.744561);
		System.out.println(location);
		getLocation("云南省昆明市盘龙区穿金路220号");
	}
}
